Skip to main content
ICT
Lesson A12 - Iterations
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

D. The for Loop page 6 of 18

  1. The for loop has the same effect as a while loop, but uses a different format. The general form of a for loop is:

for (statement1; expression2; statement3){
  statement4;
}

The for loop is typically set up as follows.

statement1 initializes the loop variable
expression2 is a boolean expression
statement3 alters the key value, usually via an increment/decrement statement
statement4 is the task to be done during each iteration

  1. Here is an example of a for loop, used to print the integers 1-10.

    for (int loop = 1; loop <= 10; loop++){
      System.out.print(loop);
    }

  2. The flow of control in a for loop is illustrated below:

Notice that after the statement is executed, control passes to the increment/decrement statement, and then back to the Boolean condition.

  1. The following is the equivalent while loop:

    int loop = 1;
    while (loop <= 10){
      System.out.print( loop);
      loop++;
    }

  1. A for loop is appropriate when the initialization value and number of iterations is known in advance. The above example of printing 10 numbers is best solved with a for loop because the number of iterations of the loop is well defined.

  2. Constructing a for loop is easier than a while loop because the key structural parts of a loop (initialization, loop boundary, and increment/decrement statement) are contained in one line. It is also easier to visually check the correctness of a for loop because it is so compact.

  3. A while loop is more appropriate when the boundary condition is tied to some input or changing value inside of the loop.

  4. Here is an interesting application of a for loop to print the alphabet:

    char letter;

    for (letter = 'A'; letter <= 'Z'; letter++){
      System.out.print(letter);
    }

    The increment statement letter++ will add one to the ASCII value of letter.

  5. A simple, but time-consuming error to find and fix is the accidental use of a null statement.

    for (loop = 1; loop <= 10; loop++); // note _;_
      System.out.print(loop);

    The semicolon placed at the end of the first line causes the for loop to do "nothing" 10 times. The output statement will only happen once after the for loop has done the null statement 10 times. The null statement can be used as a valid statement in control structures. Make sure you pay attention to your enclosing {}.

  6. There are two basic options for the variable used in the for loop. The variable can either be declared beforehand and therefore initially have a value set at another point in the program, or it can be declared and initialized within the for loop itself. Consider the following two for loops:

    int number = 1;

    for(; number <= 10; number++){
      System.out.println(number);
    }

    for(int a = 1; a <= 10; a++){
      System.out.println(a);
    }

Notice how the first statement of the first for loop is blank. This is because the int variable number has already been declared and initialized. Blank statements within the for loop are allowed. Now, both for loops appear to do the exact same thing, printing out all the numbers from 1 to 10. However, there are several key differences. In the first example, number may be changed to any number desired during the run of the program. Instead of setting it to one, a programmer could set it to the value of a function such as int number = getValidNumber(). This gives additional power to the programmer. Another difference between the two loops is the scope of the variables number and a. Because number was declared outside of the for loop, the value of number may be used after the for loop. However, a was declared within the for loop and thus will not be usable past the end bracket of the for loop.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.